﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WorkBasket.Data.Repository;
using WorkBasket.Infrastructure;
using WorkBasket.Models;
using WorkBasket.Data.Entities;
using Auth0.AspNet;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace WorkBasket.Controllers.Api
{
    public class GroupMemberController : ApiController
    {

          SOC soc = new SOC(new MyContext());

          [HttpPost]
          [Route("api/groupmember/save")]
          public IHttpActionResult Post([FromBody] GroupMember value)
          {
              try
              {
                  if (value.GroupMemberID == 0)
                  {
                      soc.GroupMembers.Add(value);
                  }
                  else
                  {
                      soc.GroupMembers.Update(value);
                  }

                  soc.CommitChanges();
 
                  return Json(new DataReturn("", value));
              }
              catch (Exception e)
              {
                  return new System.Web.Http.Results.ExceptionResult(e, this);
              }
          }
          [Route("api/groupmember/GetByKey/{GroupMemberID}")]
          [HttpGet]
          public IHttpActionResult GetByKey(int GroupMemberID)
          {

              return Json(soc.GroupMembers.GetByID(GroupMemberID));
          }
     

        [Route("api/groupmember/GetByParentKey/{GroupID}")]
        [HttpGet]
        public IHttpActionResult GetByParentKey(int GroupID)
        {
          
            return Json(soc.GroupMembers.Where(c => c.GroupID == GroupID));
        }
     
        [HttpPost]
        [Route("api/groupmember/delete")]
        public IHttpActionResult Delete([FromBody]  GroupMember value)
        {
            try
            {
                soc.GroupMembers.Delete(value.GroupMemberID);
                soc.CommitChanges();
                return Json(new DataReturn("Member Deleted", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }
         [Route("api/groupmember/Search")]
        public IHttpActionResult Search([FromBody] SearchCiteria  searchCriteria)
        {

           


            return Json(AuthFunctions.DBUserSearch(searchCriteria.Email, searchCriteria.Name));


        }
        public class SearchCiteria{
            public string Email { get; set; }
            public string Name { get; set; }
        }


         [Route("api/groupmember/GetGroupWBUsers/{GroupID}")]
        public IHttpActionResult GetGroupWBUsers(int GroupID)
        {
            return Json(soc.Execute<WBUserGroupMemberID>("GetWBUsersByGroupID @GroupID", new SqlParameter("GroupID", GroupID)));


        }
         public class WBUserGroupMemberID : WBUser
         {
             public int GroupMemberID { get; set; }
         }

    }
}
